/* Copyright (c) 2004-2022 peigen.info. All rights reserved. */

package info.peigen.hotpot.common.core.order;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.annotation.JSONField;
import com.google.common.collect.Maps;
import info.peigen.hotpot.common.core.annotation.Comment;
import info.peigen.hotpot.common.core.util.ValidationUtil;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;

import java.util.Map;

/**
 * <b>(OrderBase)</b>
 * 入参基类,必须继承
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/4/16
 */
@SuppressWarnings("DefaultAnnotationParam")
@SuperBuilder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class OrderBase implements Order {
    /**
     * 订单号，必须全局唯一，总长度22
     * 生成格式：
     * 日期时分秒（14）+分区键（2）+随机数（4）+生成顺序（2）
     * <p>
     * 例如：
     * yyyyMMddHHmmss+88+1234+01
     */
    @Comment("")
    @JSONField(name = "gid", ordinal = 0)
    @NotNull
    @Size(max = 30)
    @Pattern(regexp = "^[A-Za-z0-9]+$", message = "Gid格式错误")
    @Builder.Default
    protected final String gid = DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN) + "88" + RandomUtil.randomNumbers(4) + "01";

    /** 作为系统考虑不周全的时候放入临时用到的东西 */
    @Builder.Default
    private Map<String, Object> extend = Maps.newHashMap();

    @Override
    public void check() {
        ValidationUtil.validateParams(this);
    }
}